﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TaiwuModFramework.Common.Abstract;

namespace TaiwuModFramework.Common.Base {
    public abstract class AFwDomain {
        // 配置基本属性
        public const string ShareMemoryDefaultPath = "TaiwuModFrameworkShareMemoryDefaultPath";
        public static string ModIdStr { get; protected set; }
        


        public static ALogger Log { get; private set; } = new BaseLogger("TaiwuMF",ALogger.LEVEL.INFO, BaseLogWriter.GetLogWriter("./Mod/TaiwuModFramework/Logs/Default.log"));

        private static readonly Dictionary<Type, AConfigItemReader> ReaderDIct = new Dictionary<Type, Abstract.AConfigItemReader>();
        public static AMsgClient MsgClient { get; protected set; }
        public static AReqClient ReqClient { get; protected set; }

        protected static void RegConfigItemReader(params AConfigItemReader[] readers) {
            if(readers == null || readers.Length == 0) return;
            foreach(var reader in readers) {
                var itemType = reader.ItemType();
                if(itemType == null || ReaderDIct.ContainsKey(itemType)) return;
                ReaderDIct.Add(itemType, reader);
            }
        }
        protected static void UseLog(ALogger log) {
            if(log == null) return;
            string name0 = Log.Name, name1 = log.Name;
            string path0 = Log.LogPath, path1 = log.LogPath;

            Log.Info($"默认日志工具切换: [{name0}]-->[{name1}] \n{name0} :[{path0}]\n{name1} :[{path1}]");
            Log = log;
            Log.Info($"默认日志工具切换成功: [{name0}]-->[{name1}] \n{name0} :[{path0}]\n{name1} :[{path1}]");
        }

        public static AConfigItemReader GetItemReader(Type itemType) {
            ReaderDIct.TryGetValue(itemType, out AConfigItemReader reader);
            return reader;
        }


        public static void ClearLog() {
            try {
                String dir = Log.LogPath;
                using(FileStream s = File.Open(dir, FileMode.OpenOrCreate, FileAccess.Write)) {
                    s.Seek(0, SeekOrigin.Begin);
                    s.SetLength(0);
                    s.Flush();
                }
            } catch {

            }
        }

    }
}
